package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.Result.LdbzResult;
import com.shop.admin.service.AdminMenuService;
import com.shop.dao.LdbzAdminMenuMapper;
import com.shop.pojo.LdbzAdminMenu;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminMenuServiceImpl implements AdminMenuService {

    private static final Logger logger = LoggerFactory.getLogger(AdminMenuServiceImpl.class);

    @Autowired
    private LdbzAdminMenuMapper ldbzAdminMenuMapper;

    @Override
    public LdbzResult getAdminMenuPage(LdbzAdminMenu entity, int pn, int limit) {
        Map<String,Object> map = new HashMap<>();
        long l = ldbzAdminMenuMapper.countByEntity(entity);
        map.put("total",l);
        if (l>0&&pn>0){
            int start = (pn-1)*limit;
            QueryWrapper qw = new QueryWrapper();
            List<LdbzAdminMenu> ldbzAdminMenus = ldbzAdminMenuMapper.selectByEntity(entity, start, limit);
            map.put("list",ldbzAdminMenus);
        }
        return LdbzResult.build(0,"",map);
    }

    @Override
    public LdbzResult getMenuTree() {
        return LdbzResult.build(0,"",ldbzAdminMenuMapper.getMenuTree());
    }

    @Override
    public LdbzResult selectByKey(Long id) {
        return LdbzResult.ok(ldbzAdminMenuMapper.selectById(id));
    }

    @Transactional
    @Override
    public LdbzResult deleteByKey(String id) {
        if (StringUtils.contains(id,",")){
            List<Long> ids = new ArrayList<>();
            for (String _id : StringUtils.split(id,",")){
                ids.add(Long.valueOf(_id));
            }
            return LdbzResult.ok(ldbzAdminMenuMapper.deleteByKeys(ids));
        }else {
            return LdbzResult.ok(ldbzAdminMenuMapper.deleteById(id));
        }
    }

    @Override
    public LdbzResult insertByEntity(LdbzAdminMenu entity) {
        if (ldbzAdminMenuMapper.insert(entity)>=1)
            return LdbzResult.ok();
        else
            return LdbzResult.build(500,"添加失败");
    }

    @Override
    public LdbzResult updateByKey(LdbzAdminMenu entity) {
        return LdbzResult.ok(ldbzAdminMenuMapper.deleteById(entity));
    }
}
